//
//  XGRecommendUserCollectionViewCell.swift
//  TodayNews
//
//  Created by monkey on 2020/2/20.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher
import MBProgressHUD

class XGRecommendUserCollectionViewCell: UICollectionViewCell, XGRegisterReusableViewProtocol
{
    var userCard: XGUserCardModel? {
        didSet {
            if let avatarUrl = userCard?.userInfo?.avatar_url {
                userIconImageView.kf.setImage(with: URL(string: avatarUrl), options:  [.processor(RoundCornerImageProcessor(cornerRadius:40, targetSize: CGSize(width: 80, height: 80)))])
            }
            
            nameLabel.text = userCard?.show_name
            descriptionLabel.text = userCard?.recommend_reason
            verifyImageView.isHidden = userCard?.userInfo?.user_verified != true
        }
    }
    /// 视图模型
    var userDetailViewModel: XGUserDetailViewModel?
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 头像
    private lazy var userIconImageView: UIImageView = UIImageView()
    /// 名称
    private lazy var nameLabel: UILabel = UILabel(text: "测试", textColor: UIColor.black, fontSize: 15, textAlignment: .center)
    /// 推荐描述
    private lazy var descriptionLabel: UILabel = UILabel(text: "测试", textColor: UIColor.black, fontSize: 12, textAlignment: .center)
    /// 认证标识
    private lazy var verifyImageView: UIImageView = UIImageView(image: UIImage(named: "all_v_avatar_18x18_"))
    /// 关注按钮
    private lazy var followingButton: UIButton = {
        let button = UIButton(title: "关注", selectedTitle: "已关注", fontSize: 15, normalColor: UIColor.white, target: self, action: #selector(followRelationAction(button:)))
        button.setTitleColor(UIColor.lightGray, for: .selected)
        button.setBackgroundImage(UIColor(r: 227, g: 90, b: 86).toImage(), for: .normal)
        button.setBackgroundImage(UIColor.white.toImage(), for: .selected)
        button.layer.borderColor = UIColor(white: 0.9, alpha: 1).cgColor
        button.layer.borderWidth = 1
        return button
    }()
}

// MARK: - 私有扩展

private extension XGRecommendUserCollectionViewCell
{
    /// 关注用户事件
    @objc func followRelationAction(button: UIButton) -> Void
    {
        userDetailViewModel?.isFollowRelationChanged = true
        let userId = userCard?.userInfo?.user_id ?? 0
        if button.isSelected { // 当前选中状态 取消关注用户
            userDetailViewModel?.unfollowRelation(userId: userId, completionHandle: { (isSuccess) in
                if isSuccess {
                    button.isSelected = !button.isSelected
                } else {
                    MBProgressHUD.showErrorTips(text: "取消关注失败,当前网络不佳或今日操作次数已满!")
                }
            })
        } else { // 当前非选中状态 关注用户
            userDetailViewModel?.followRelation(userId: userId, completionHandle: { (isSuccess) in
                if isSuccess {
                    button.isSelected = !button.isSelected
                }
            })
        }
    }
    
    func setUpUI() -> Void
    {
        backgroundColor = UIColor.white
        
        contentView.addSubview(userIconImageView)
        contentView.addSubview(verifyImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(descriptionLabel)
        contentView.addSubview(followingButton)
        
        userIconImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 80, height: 80))
        }
        verifyImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userIconImageView.snp.right).offset(-10)
            make.centerY.equalTo(userIconImageView.snp.bottom).offset(-10)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIconImageView.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
        }
        descriptionLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.lessThanOrEqualTo(ceil(descriptionLabel.font.lineHeight * 2))
        }
        followingButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-5)
            make.size.equalTo(CGSize(width: 100, height: 30))
        }
    }
}
